/**
 * @param {number[][]} grid
 * @return {number}
 */
var equalPairs = function (grid) {
  let count = 0, map = new Map(), n = grid.length, arr = []
  for (let i = 0; i < n; i++) {
    map.set(grid[i].join(''), map.get(grid[i].join(''))+1 || 1)
  }

  for (let i = 0; i < n; i++) {
    let res = ''
    for (let j = 0; j < n; j++) {
      res += grid[j][i]
    }
    if (map.has(res)) {
      count+=map.get(res)
      arr.push(res)
    }
  }
  return count
};

equalPairs([[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]])